import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterapp/src/page/common/app_upgrade.dart';
import 'package:flutterapp/src/page/common/user_helper.dart';
import 'package:flutterapp/src/page/login/login_page.dart';
import 'package:flutterapp/src/utils/log_utils.dart';
import 'package:flutterapp/src/utils/navigator_utils.dart';
import 'package:package_info/package_info.dart';

class SettingPage extends StatefulWidget {
  final StreamController loginStreamController;
  SettingPage(this.loginStreamController);
  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("设置中心"),
      ),
      body: ListView(
        children: [
          // 检测更新
          buildCheckVersion(),
          //  登录与退出登录
          buildLoginWidget(),
        ],
      ),
    );
  }

  buildLoginWidget() {
    bool isLogin = UserHelper.getInstance.isLogin;
    return ListTile(
      title: Text(isLogin ? "退出登录" : "去登录"),
      //右侧按钮
      trailing: Icon(Icons.arrow_forward_ios),
      //左侧箭头
      leading: Icon(Icons.verified_user),
      //点击事件
      onTap: () {
        if (isLogin) {
          exitFunction();
        } else {
          NavigatorUtils.pushPage(
              context: context,
              targetPage: LoginPage(widget.loginStreamController));
        }
      },
    );
  }

  void exitFunction() async {
    bool isExit = await showCupertinoDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          title: Text("温馨提示"),
          content: Container(
            padding: EdgeInsets.all(16),
            child: Text("确定退出吗？"),
          ),
          actions: [
            CupertinoDialogAction(
              child: Text("取消"),
              onPressed: () {
                Navigator.of(context).pop(false);
              },
            ),
            CupertinoDialogAction(
              child: Text("退出", style: TextStyle(color: Colors.red)),
              onPressed: () {
                // 清除用户本地缓存信息
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      },
    );
    if (isExit) {
      UserHelper.getInstance.clear();
      Navigator.of(context).pop(true);
    }
  }

  // 构建版本更新
  buildCheckVersion() {
    return ListTile(
      title: Text("检测更新"),
      //右侧按钮
      trailing: Icon(Icons.arrow_forward_ios),
      //左侧箭头
      leading: Icon(Icons.web_sharp),
      //点击事件
      onTap: () {
        checkVersion();
      },
    );
  }
  // 检测版本更新

  void checkVersion() async {
    // 获取当前app的版本信息
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String appName = packageInfo.appName;
    String packageName = packageInfo.packageName;
    String version = packageInfo.version;
    String buildNumber = packageInfo.buildNumber;

    LogUtils.e("appName $appName");
    LogUtils.e("packageName $packageName");
    LogUtils.e("version $version");
    LogUtils.e("buildNumber $buildNumber");
    checkAppVersion(context, showToast: true);
  }
}
